layui.use(['element','laypage', 'layer','carousel', 'form'], function(){
    var element = layui.element;
    var laypage = layui.laypage
        ,layer = layui.layer;
    var carousel = layui.carousel
        ,form = layui.form;



    //图片轮播
    carousel.render({
        elem: '#test10'
        ,width: '100%'
        ,height: '320px'
        ,interval: 5000
    });

    /**
     * 导航栏菜单高亮显示
     */
    var type = $("#type").val();
    $("#menu"+type).css("color","#45B6F7");
    $("#menu"+type).addClass("layui-this");

});

$(function () {

    initLayPage();

})


function initLayPage(pageConf) {
    if(!pageConf){
        pageConf = {};
        pageConf.limit = 10;
        pageConf.page = 1;
    }

    $.ajax({
        type:"POST"
        ,url:"/getIndexData"
        ,data:{
            "page":pageConf.page,
            "limit":pageConf.limit
        }
        ,success:function (res) {
            layui.use(['element','laypage', 'layer'], function(){
                var element = layui.element;
                var laypage = layui.laypage
                    ,layer = layui.layer;

                laypage.render({
                    elem: 'page'
                    ,count: res.count
                    ,curr:pageConf.page
                    ,limit:pageConf.limit
                    ,theme: '#1E9FFF'
                    ,jump:function (obj,first) {
                        pageConf.page=obj.curr;  //改变当前页码
                        pageConf.limit=obj.limit;
                        //首次不执行
                        if(!first){
                            initLayPage(pageConf);  //加载数据
                        }
                    }
                });

                fillTable(res.data,(pageConf.page - 1) * pageConf.limit); //页面填充

            });




        }
        ,error:function () {
            alert("服务器错误！");
        }
    });


}

function fillTable(rows,num) {

    var html = '';

    if(rows.length==0){

        html += "<h1 style='text-align: center;color: red'>暂无数据！</h1>"

        $(".article_contents").empty().append(html);

        return ;
    }

    for(var i=0;i<rows.length;i++){
        html += '     <div class="layui-fluid article" > ';
        html += '     <div class="layui-row"> ';
        html += '       <div class="layui-col-md4"> ';
        html += '            <div class="coverImg"> ';
        html += '                <img src='+rows[i].cover+' style="width: 220px;height: 150px;" class="img-lg"> ';
        html += '             </div>';
        html += '        </div> ';
        html += '       <div class="layui-col-md8"> ';
        html += '           <div class="articleContent"> ';
        html += '               <div class="title hover-title"  > ';
        html += '                   <div style="display: inline"> ';

        //项目是否是今日的
        if(rows[i].isToday == "1"){
            html += '                       <span class="layui-badge article-label" >今日更新</span> ';
        }


        for(var j=0;j<rows[i].typeList.length;j++){
            //包赔项目type标红
            if(rows[i].typeList[j].typeName.match("包赔")){
                html += '                       <span class="layui-badge  article-label layui-hide-xs" >'+rows[i].typeList[j].typeName+'</span> ';
            }else{
                html += '                       <span class="layui-badge layui-bg-blue  article-label layui-hide-xs" >'+rows[i].typeList[j].typeName+'</span> ';
            }

         }

        html += '                   </div> ';
        html += '                   <div style="display: inline" onclick="desc('+rows[i].id+',\''+rows[i].type+'\')" title="'+rows[i].title+'" >'+rows[i].title+'</div> ';
        html += '                   </div> ';
        html += '                   <div class="meta"> ';
        html += '                       <i class="layui-icon meta-icon layui-icon-date" >'+rows[i].showCreateTime+'</i> ';
        html += '                       <i class="layui-icon meta-icon layui-icon-read" >阅读('+rows[i].readMount+')</i> ';
        html += '                   </div> ';
        html += '                   <div class="articleInfo layui-hide-xs">'+rows[i].summary+'</div> ';
        html += '                   <div class="article-labels"> ';
        // html += '                       <span class="layui-badge layui-bg-green"  th:each="l:${p.labelList}" th:text="${l.labelName}"></span> ';

        //遍历标签
        for(var a=0;a<rows[i].labelList.length;a++){
            html += '                       <span class="layui-badge layui-bg-green">'+rows[i].labelList[a].labelName+'</span>';
        }

        html += '                    </div> ';
        html += '               </div> ';
        html += '            </div> ';
        html += '       </div> ';
        html += '   </div> ';
        html += '   </div> ';

    }

    var pageDiv = $("#pageDiv");
    var fire = $(".fire");
    $(".article_contents").empty().append(fire);
    $(".article_contents").append(html);
    $(".article_contents").append(pageDiv);

}



/**
 * 查看文章详情页面
 * @param id
 */
function  desc(id,type) {
    if("B"  == type){
        //包赔项目
        location.href = "/article/baopei_desc/0/"+id;
    }else{
        //非包赔项目
        location.href = "/article/desc/0/"+id;
    }
}




